javascript:
/*	twcheese_commandHauls.js
 *	Commands Overview - show returning hauls
 *	version 2.0_01
 *	game compatability: version 9750 6.5
 *	market: uk
 *	author Nick Toby (cheesasaurus@gmail.com)

 *	use script on: game.php?screen=overview_villages&mode=commands&type=return (the commands overview, with the return filter on)
 *	effect: includes 'haul' as part of the information for the listed commands. Also shows a sum at the bottom
 
 *	Copyright (C) 2010  Nick Toby

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/
 */
 
/*==== templates ====*/

	/**
	 *	declarations for command template
	 */
	function twcheese_Command()
	{
		this.command_id;
		this.origin_player_id;
		this.origin_village_id;
		this.origin_x;
		this.origin_y;
		this.dest_player_id;
		this.dest_village_id;
		this.dest_x;
		this.dest_y;
		this.duration;
		this.arrival;
		this.timber = '0';
		this.clay = '0';
		this.iron = '0';
	}

/*==== functions ====*/

	/**
	 *	includes haul information in the commands table
	 *	@param	gameDoc:HTMLDocument	the page generated by game.php?screen=overview_villages&mode=commands&type=return
	 */
	function twcheese_includeHaulInfo(gameDoc)
	{		
		var commandsTable = gameDoc.getElementById('commands_table');
		var fillerSpan = commandsTable.rows[0].cells.length;
		
		/*==== add haul headers to the commands table ====*/
		var timberHeader = document.createElement('th');
		commandsTable.rows[0].appendChild(timberHeader);
		timberHeader.innerHTML = '<img src="/graphic/holz.png?1" title="Wood" alt="Timber" />';
		
		var clayHeader = document.createElement('th');
		commandsTable.rows[0].appendChild(clayHeader);
		clayHeader.innerHTML = '<img src="/graphic/lehm.png?1" title="Clay" alt="Clay" />';
		
		var ironHeader = document.createElement('th');
		commandsTable.rows[0].appendChild(ironHeader);
		ironHeader.innerHTML = '<img src="/graphic/eisen.png?1" title="Iron" alt="Iron" />';
		
		/*==== append resources hauled to each row in the commands table. Sum resources while doing so ====*/
		var timberTotal = 0;
		var clayTotal = 0;
		var ironTotal = 0;
		
		for(var i=1; i < commandsTable.rows.length; i++)
		{
			var commandUrl = commandsTable.rows[i].cells[0].getElementsByTagName('a')[0].href;
			var command = twcheese_scrapeCommand(twcheese_requestDocumentBody(commandUrl));
			
			var timberCell = commandsTable.rows[i].insertCell(-1);
			timberCell.innerHTML = command.timber;
			timberTotal += Number(command.timber);
			
			var clayCell = commandsTable.rows[i].insertCell(-1);
			clayCell.innerHTML = command.clay;
			clayTotal += Number(command.clay);
			
			var ironCell = commandsTable.rows[i].insertCell(-1);
			ironCell.innerHTML = command.iron;
			ironTotal += Number(command.iron);
		}
		
		/*==== add row with total resources ====*/
		var resTotalRow = commandsTable.insertRow(-1);
		for(var i=0; i < fillerSpan; i++)
			resTotalRow.insertCell(-1);
		resTotalRow.cells[fillerSpan - 1].innerHTML = 'Total:';
		
		var timberCell = resTotalRow.insertCell(-1);
		timberCell.innerHTML = timberTotal;
		
		var clayCell = resTotalRow.insertCell(-1);
		clayCell.innerHTML = clayTotal;
		
		var ironCell = resTotalRow.insertCell(-1);
		ironCell.innerHTML = ironTotal;
	}
	
	/**
	 *	scrapes a command page for the command info and returns it as a twcheese_Command object
	 *	@param	gameDoc:HTMLDocument	the page generated by game.php?screen=command_info&id=x&type=own
	 *	@return command:twcheese_Command	an object representing the command.
	 */
	function twcheese_scrapeCommand(gameDoc)
	{
		var command = new twcheese_Command();		
		var cells = gameDoc.getElementsByTagName("td");
		for(var i = 0; i < cells.length; i++)
		{
			if(cells[i].innerHTML == "Arrival:")
			{				
				command.arrival = cells[i+1].innerHTML;
				/* todo: convert to Date object */
			}
			if(cells[i].innerHTML == "Haul:")
			{
				var haul = twcheese_resElementToNumbers(cells[i+1]);
				command.timber = haul[0];
				command.clay = haul[1];
				command.iron = haul[2];
							
			}
			
			/*==== todo ====
			command.origin_player_id;
			command.origin_village_id;
			command.origin_x;
			command.origin_y;
			command.dest_player_id;
			command.dest_village_id;
			command.dest_x;
			command.dest_y;
			command.duration;
			*/
		}		
		return command;
	}
	
	/**
	 *	requests the body from an html document and returns it as an HTML element
	 *	@param	targetUrl	the url of the page to get the document body from
	 *	@return	requestedDocumentBody:HTMLBodyElement
	 */
	function twcheese_requestDocumentBody(targetUrl)
	{
		var requestedDocumentBody;
		var xmlhttp;
		if (window.XMLHttpRequest)
			xmlhttp=new XMLHttpRequest();
		else
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET",targetUrl,false);
		xmlhttp.send("");
		requestedDocumentBody = document.createElement("body");
		requestedDocumentBody.innerHTML = xmlhttp.responseText;
		return requestedDocumentBody;
	}
	
	/**
	 * reads a an HTMLElement with the timber count, clay count, and iron count, and converts it to an array of Numbers
	 * @param	resElement:HTMLElement	the html of the resources
	 * @return	resources:Array(timber:Number,clay:Number,iron:Number)
	 */
	function twcheese_resElementToNumbers(resElement)
	{
		var resNames = new Array('Wood','Clay','Iron');
		var resources = new Array(0,0,0);		
		
		/*==== remove the grey periods ====*/
		while(resElement.getElementsByTagName('span').length > 0)
			resElement.removeChild(resElement.getElementsByTagName('span')[0]);
		
		/*==== set resources ====*/
		var images = resElement.getElementsByTagName('img');
		
		if(navigator.appName == 'Microsoft Internet Explorer') /* internet explorer */
		{
			for(var i=0; i < images.length; i++)
			{
				/*==== if timber image is found, set timber ====*/
				if(images[i].title == resNames[0])
					resources[0] = Number(images[i].nextSibling.data);
				
				/*==== if clay image is found, set clay ====*/
				if(images[i].title == resNames[1])
					resources[1] = Number(images[i].nextSibling.data);
				
				/*==== if iron image is found, set iron ====*/
				if(images[i].title == resNames[2])
					resources[2] = Number(images[i].nextSibling.data);
			}
		}
		else /* if(navigator.appName == 'Opera' || navigator.appName == 'Netscape') //opera, netscape */
		{
			for(var i=0; i < images.length; i++)
			{			
				/*==== if timber image is found, set timber ====*/
				if(images[i].title == resNames[0])
					resources[0] = Number(images[i].nextSibling.wholeText);
				
				/*==== if clay image is found, set clay ====*/
				if(images[i].title == resNames[1])
					resources[1] = Number(images[i].nextSibling.wholeText);
				
				/*==== if iron image is found, set iron ====*/
				if(images[i].title == resNames[2])
					{
					resources[2] = images[i].nextSibling.wholeText;
					var test = resources[2];
					var test2 = test.split(" ");
					resources[2] = Number(test2[0]);
					}
			}
		}
		return resources;
	}
	
/*==== main ====*/

	if(!twcheese_haulsIncluded)
	{
		if(game_data.screen == 'overview_villages' && game_data.mode == 'commands')
		{
			var addHauls = confirm('Jagerblue owns@ dost thou wish hauls to be included on thine screen?');
			if(addHauls)
			{
				twcheese_includeHaulInfo((window.frames.length>0)?window.main.document:document);
				var twcheese_haulsIncluded = true;
				alert('Your highness, I have finished my task. May the cheese be with you.');
			}
		}
		else
			alert('To use this, you must be on the commands overview. It\'s recommended to use the \'return\' filter, since outgoing troops don\'t carry resources :)');
	}
	void(0);